package net.maxupeng.asm74.util;

import net.maxupeng.asm74.antlr.ASM74Lexer;
import net.maxupeng.asm74.antlr.ASM74Parser;
import org.antlr.v4.runtime.*;

public class AntlrUtil {
    public static ASM74Parser.ProgContext parse(String content) {
        BaseErrorListener errorListener = new BaseErrorListener() {
            @Override
            public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
                                    int line, int charPositionInLine, String msg, RecognitionException e) {
                System.err.println("line " + line + ":" + charPositionInLine + " " + msg);
                System.exit(1);
            }
        };

        ASM74Lexer lexer = new ASM74Lexer(CharStreams.fromString(content));
        lexer.addErrorListener(errorListener);
        CommonTokenStream token = new CommonTokenStream(lexer);
        ASM74Parser parser = new ASM74Parser(token);
        parser.addErrorListener(errorListener);
        return parser.prog();
    }
}
